package com.example.application.views.regiest;


import com.example.application.bean.ResultBean;
import com.example.application.bean.User;
import com.example.application.config.URLConfig;
import com.example.application.views.main.MainView;
import com.example.application.views.util.MyHttpClientUtil;
import com.example.application.views.util.VerifyUtil;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.PasswordField;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Route(value = "enroll")
@PageTitle("注册")
public class EnrollView extends Div {

    public EnrollView(){

        VerticalLayout verticalLayout = new VerticalLayout();
        //创建一个水平布局
        HorizontalLayout layout = new HorizontalLayout();

        FormLayout formLayout = new FormLayout();
        //设置距离上面300px
        layout.getStyle().set("margin-top","10px");

        //账号
        TextField user = new TextField();
        user.setLabel("账号");
        user.setPlaceholder("请输入账号");
       // user.getStyle().set("width","100px");

        //密码
        PasswordField pwd = new PasswordField();
        pwd.setLabel("密码");
        pwd.setPlaceholder("请输入密码");

        PasswordField surePwd = new PasswordField();
        surePwd.setLabel("确认密码");
        surePwd.setPlaceholder("请输入密码");
        surePwd.getStyle().set("margin-left", "50px");

        TextField email = new TextField();
        email.setLabel("请输入邮箱");
        email.setPlaceholder("请输入邮箱");
        email.getStyle().set("margin-left", "50px");

        TextField name = new TextField();
        name.setLabel("请输入姓名");
        name.setPlaceholder("请输入姓名");

        //手机号
        TextField phone = new TextField();
        phone.setLabel("手机号");
        phone.setPlaceholder("请输入手机号");
        phone.getStyle().set("margin-left", "50px");

        ComboBox<String> comboBox = new ComboBox<>();
        comboBox.setLabel("选择注册身份");
        comboBox.setWidth("450px");
        List<String> list = new ArrayList();
        list.add("门店管理员");
        list.add("供应商管理员");

        comboBox.setItems(list);
        comboBox.setValue(list.get(0));

        HorizontalLayout h1 = new HorizontalLayout(user, phone);
        HorizontalLayout h2 = new HorizontalLayout(pwd, surePwd);
        HorizontalLayout h3 = new HorizontalLayout(name, email);
        HorizontalLayout h4 = new HorizontalLayout(comboBox);
        verticalLayout.add(h1, h2, h3, h4);
        formLayout.add(verticalLayout);

        Button retur = new Button("返回");
        retur.getStyle().set("margin-left","20px");
        retur.getStyle().set("width", "50px");
        retur.addClickListener(e->{
            UI.getCurrent().navigate("login");
        });

        Button enroll = new Button("注册");
        enroll.getStyle().set("margin-left","50px");
        enroll.getStyle().set("width", "50px");

        enroll.addClickListener(e->{
            if(!VerifyUtil.lengthVerify(user.getValue().trim())){
                UI.getCurrent().getPage().executeJs("alert('用户名长度不能大于10');");
            }else if(!VerifyUtil.telePhoneNumVerify(phone.getValue().trim())){
                UI.getCurrent().getPage().executeJs("alert('电话格式错误');");
            }else if(!VerifyUtil.lengthVerify(name.getValue().trim())){
                UI.getCurrent().getPage().executeJs("alert('姓名长度不能大于10');");
            }else if(! pwd.getValue().trim().equals(surePwd.getValue().trim())){
                UI.getCurrent().getPage().executeJs("alert('密码不一致');");
            }else if(!VerifyUtil.emailVerify(email.getValue().trim())){
                UI.getCurrent().getPage().executeJs("alert('邮箱格式错误');");
            }else{
                ResultBean<String> resultBean = new ResultBean<>();
                String url = URLConfig.REGISTER;

                User user1 = new User();
                user1.setIdentity(comboBox.getValue().trim());
                user1.setLoginName(user.getValue().trim());
                user1.setPassword(pwd.getValue().trim());
                user1.setTelNum(phone.getValue().trim());
                user1.setEmail(email.getValue().trim());
                user1.setName(name.getValue().trim());

                try (CloseableHttpClient httpClient = HttpClients.createDefault()){

                    ObjectMapper mapper = new ObjectMapper();

                    HttpPost httpPost = new HttpPost(url);

                    String jsonString = mapper.writeValueAsString(user1);
                    StringEntity entity1 = new StringEntity(jsonString,"utf-8");
                    httpPost.setHeader("Content-Type", "application/json;charset=utf8");
                    httpPost.setEntity(entity1);

                    CloseableHttpResponse response = httpClient.execute(httpPost);
                    HttpEntity entity = response.getEntity();
                    String json = EntityUtils.toString(entity,"utf-8");

                    JavaType javaType = mapper.getTypeFactory().constructParametricType(ResultBean.class, String.class);

                    resultBean = mapper.readValue(json, javaType);

                    if(resultBean.getCode() == 0){
                        UI.getCurrent().getPage().executeJs("alert('"+ resultBean.getMsg() +"');");
                    }else{
                        UI.getCurrent().getPage().executeJs("alert('注册成功，等待管理员审核');");
                        UI.getCurrent().navigate("login");
                    }

                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

        });

        layout.add(retur,enroll);

        verticalLayout.add(layout);
        verticalLayout.getStyle().set("width","390px");
        verticalLayout.getStyle().set("margin-top","50px");
        verticalLayout.getStyle().set("margin-left","300px");

        //让窗体居中
        verticalLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
       // layout.add(verticalLayout);

        //在垂直布局添加到容器中
        this.add(verticalLayout);
    }
}
